/*
 * @Description: CDN配置
 * @Author: seadon
 * @LastEditors: qiyu qiyu@byteluck.com
 * @Date: 2022-03-07 20:04:11
 * @LastEditTime: 2022-10-13 22:38:39
 */

const fs = require('fs')
const path = require('path')
const {
	generateCDNPathList,
	getTheme,
} = require('@byteluck-fe/global-shared/dist/umd/compile/index.compile.umd')

const processEnv = process.env
const basePath = processEnv.VUE_APP_PUBLIC_PATH
const cdnPath = processEnv.VUE_APP_CDN_PATH

// 读取 cdn.json 文件，解析成对象
let cdnJSON = fs.readFileSync(
	path.resolve(__dirname, '../public/cdn.json'),
	'utf-8'
)
const cdnObject = JSON.parse(cdnJSON)

// 合并对象
function handleConcatCDN(target, source) {
	const concatObj = JSON.parse(JSON.stringify(target))
	for (const key in concatObj) {
		if (source[key] && source[key].length)
			concatObj[key] = Array.from(new Set(concatObj[key].concat(source[key])))
	}
	return concatObj
}

const timestamp = new Date().getTime().toString()
// 开发与正式环境都需加载的资源
const commonCDN = {
	externals: [],
	js: generateCDNPathList(
		cdnPath,
		cdnObject,
		['jsencrypt', 'browser-probing', 'vue'],
		'js'
	),
	/* asyncJs: [
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.js?${timestamp}`,
		`${cdnPath}/lib/ok-wc-ui/3.3.22/dist/ok-wc-ui.umd.js`,
	], */
	asyncJs: [
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.js?${timestamp}`,
		`${cdnPath}/iconfonts/portal-fonts/iconfont.js?${timestamp}`,
		...generateCDNPathList(cdnPath, cdnObject, ['ok-wc-ui'], 'js'),
	],
	deferJs: [],
	/* css: [
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.css?${timestamp}`,
		`${cdnPath}/lib/ok-wc-ui/3.3.22/dist/style.css`,
	], */
	css: [
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.css?${timestamp}`,
		`${cdnPath}/iconfonts/portal-fonts/iconfont.css?${timestamp}`,
		...generateCDNPathList(cdnPath, cdnObject, ['ok-wc-ui'], 'css'),
	],
}
// `${cdnPath}/lib/byteluck/byteluck.theme.blue.ant.css?${timestamp}`,

// PC端资源
const desktopCommonCDN = handleConcatCDN(commonCDN, {
	// asyncJs: [`${cdnPath}/libs/tinymce-5.8.0/tinymce.min.js`],
	asyncJs: generateCDNPathList(cdnPath, cdnObject, ['tinymce'], 'js'),
})

// 移动端资源
const mobileCommonCDN = handleConcatCDN(commonCDN, {
	// css: [`${cdnPath}/libs/vant-3.6.4/index.css`],
	css: generateCDNPathList(cdnPath, cdnObject, ['vant'], 'css'),
})

// login资源
const loginCommonCDN = {
	js: [
		...generateCDNPathList(
			cdnPath,
			cdnObject,
			['jsencrypt', 'browser-probing', 'moment', 'dayjs', 'vue', 'antdv'],
			'js'
		),
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.js?${timestamp}`,
	],
	css: [
		`${cdnPath}/lib/byteluck/byteluck.theme.blue.ant.css?${timestamp}`,
		`${cdnPath}/iconfonts/lowcode-fonts/iconfont.css?${timestamp}`,
	],
}

// ddlogin.html 和 zyddlogin.html 加载资源
const ddloginCommonCDN = {
	js: [...generateCDNPathList(cdnPath, cdnObject, ['vue'], 'js')],
	css: [],
}

// authorization.html 加载资源
const authorizationCommonCDN = {
	js: [
		...generateCDNPathList(
			cdnPath,
			cdnObject,
			['lodash', 'dayjs', 'moment', 'vue', 'antdv'],
			'js'
		),
	],
	css: [...generateCDNPathList(cdnPath, cdnObject, ['antdv'], 'css')],
}

// 扩展依赖，仅正式环境加载CDN
const externalsCDN = {
	externals: [
		{
			vue: 'Vue',
			'ant-design-vue': 'antd',
			vant: 'vant',
			moment: 'moment',
			lodash: '_',
			dd: 'dd',
		},
	],
	/* js: [
		`${cdnPath}/libs/lodash-4.17.21/lodash.min.js`,
		`${cdnPath}/libs/vue-3.2.26/vue.global.prod.js`,
	], */
	js: generateCDNPathList(cdnPath, cdnObject, ['lodash', 'vue'], 'js'),
}

// PC端扩展依赖
const desktopExternalsCDN = handleConcatCDN(externalsCDN, {
	/* js: [
		`${cdnPath}/libs/moment-2.27.0/min/moment-with-locales.min.js`,
		`${cdnPath}/libs/antdv-2.1.6/antd.min.js`,
	], */
	js: generateCDNPathList(
		cdnPath,
		cdnObject,
		['moment', 'dayjs', 'antdv'],
		'js'
	),
})

// 移动端扩展依赖
const mobileExternalsCDN = handleConcatCDN(externalsCDN, {
	// js: [`${cdnPath}/libs/vant-3.6.4/vant.min.js`],
	js: generateCDNPathList(cdnPath, cdnObject, ['vant'], 'js'),
})

// 生产环境上使用合并后的CDN
const desktopAssetsCDN = handleConcatCDN(desktopCommonCDN, desktopExternalsCDN)
const mobileAssetsCDN = handleConcatCDN(mobileCommonCDN, mobileExternalsCDN)

module.exports = {
	handleConcatCDN,
	desktopCommonCDN,
	mobileCommonCDN,
	desktopAssetsCDN,
	mobileAssetsCDN,
	externalsCDN,
	cdnObject,
	generateCDNPathList,
	getTheme,
	loginCommonCDN,
	ddloginCommonCDN,
	authorizationCommonCDN,
}
